package com.zhu.storesmanager.utils;


import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.util.Log;

import com.zhu.storesmanager.R;
import com.zhu.storesmanager.models.ProjectDataItem;
import com.zhu.storesmanager.models.SyncModel;
import com.zhu.storesmanager.models.UserData;
import com.zhu.storesmanager.models.VisitTaskInfo;
import com.zhu.storesmanager.provider.StoresManagerSettings;

public class DataUtils {

    public static final String SPLIT_LINE = "&";
    public static final String SPLIT_ROW = ";";
    public static final String SPLIT_TEXT = ",";
    public static final String CHECK_TIME_SATUS_SERVER = "Server"; //check时间状态 表示服务器时间
    public static final String CHECK_TIME_SATUS_APP = "App"; //check时间状态 表示手机本地时间
    
	public static UserData mUserData = null;
	public static SyncModel.QueryVisitContentResult mQueryVisitContentResult = null; //查询的所有数据
	public static SyncModel.DesDataVisitContentSales mDesDataVisitContentSales = null; //销量数据
	public static SyncModel.ProjectResult mProjectResult = null; //拜访项目标签

	/**
	 * 获取主界面按钮内容数据
	 * @param type 用户类别
	 * @param buttonIndex 按钮序号
	 * @param array 数据数组 0位 文本 1位图片
	 * @return 0 获取失败 1获取内容成功
	 */
	public static int getDataButton(int type, int buttonIndex, int[] array){
		switch(type){
			case UserData.USERTYPE_SALESPERSON: //促销员
				if(buttonIndex == 1){//签入
					array[0] = R.string.main_singin;
					array[1] = R.drawable.incon_index_singin;
					return 1;
				}else if(buttonIndex == 2){//提醒
					array[0] = R.string.main_notification;
					array[1] = R.drawable.incon_index_remind;
					return 1;
				}else if(buttonIndex == 3){//同步
					array[0] = R.string.main_sync;
					array[1] = R.drawable.incon_index_synchronous;
					return 1;
				}else if(buttonIndex == 4){ //上传
					array[0] = R.string.main_putdata;
					array[1] = R.drawable.incon_index_putdata;
					return 1;
				}else if(buttonIndex == 5){ //查询
					array[0] = R.string.main_search;
					array[1] = R.drawable.incon_index_search;
					return 1;
				}else if(buttonIndex == 6){ //任务
					array[0] = R.string.main_task;
					array[1] = R.drawable.incon_index_task;
					return 1;
				}
			    break;
			case UserData.USERTYPE_SUPERVISOR: //督导
			    if(buttonIndex == 1){//签入
                    array[0] = R.string.main_visit;
                    array[1] = R.drawable.incon_index_singin;
                    return 1;
                }else if(buttonIndex == 2){//提醒
                    array[0] = R.string.main_notification;
                    array[1] = R.drawable.incon_index_remind;
                    return 1;
                }else if(buttonIndex == 3){//同步
                    array[0] = R.string.main_sync;
                    array[1] = R.drawable.incon_index_synchronous;
                    return 1;
                }
			    break;
			case UserData.USERTYPE_ADMIN: //管理员
			    if(buttonIndex == 1){ //查询
					array[0] = R.string.main_search;
					array[1] = R.drawable.incon_index_search;
					return 1;
			    }
			    break;
		}
		return 0;
	}

	/**
     * 保存门店签入信息
     * @param context 上下问
     * @param visitTaskInfo 拜访门店信息
     * @param lat 经纬度
     * @param lng 经纬度
     */
    public static boolean saveStoresCheckinInfo(Context context, VisitTaskInfo visitTaskInfo, double lat, double lng, boolean isNetWorkTime){
        try{
            ContentValues values = new ContentValues();
            values.put(StoresManagerSettings.SupervisorVisitTaskColumns.STOREID, visitTaskInfo.mStoreId);
            values.put(StoresManagerSettings.SupervisorVisitTaskColumns.CHECKIN_LAT, lat);
            values.put(StoresManagerSettings.SupervisorVisitTaskColumns.CHECKIN_LNG, lng);
            values.put(StoresManagerSettings.SupervisorVisitTaskColumns.CHECKIN_TIME, visitTaskInfo.mCheckinTime);
            if(isNetWorkTime){
            	values.put(StoresManagerSettings.SupervisorVisitTaskColumns.CHECKIN_TIME_STATUS, CHECK_TIME_SATUS_SERVER);
            }else{
            	values.put(StoresManagerSettings.SupervisorVisitTaskColumns.CHECKIN_TIME_STATUS, CHECK_TIME_SATUS_APP);
            }
            if(Actions.DEBUG){
                Log.e("zhu","checkin id="+visitTaskInfo.mDbId);
            }
            if(visitTaskInfo.mDbId == -1){
                Uri uri = context.getContentResolver().insert(StoresManagerSettings.SupervisorVisitTaskColumns.CONTENT_URI, values);
                visitTaskInfo.mDbId = Long.parseLong(uri.getPathSegments().get(1));
            }else{
                context.getContentResolver().update(StoresManagerSettings.SupervisorVisitTaskColumns.getContentUri(visitTaskInfo.mDbId, false), values, null, null);
            }
            return true;
        }catch(Exception e){
        	e.printStackTrace();
            return false;
        }
    }

    /**
     * 保存门店签出信息
     * @param context 上下问
     * @param visitTaskInfo 拜访门店信息
     * @param lat 经纬度
     * @param lng 经纬度
     */
    public static boolean saveStoresCheckoutInfo(Context context, VisitTaskInfo visitTaskInfo, double lat, double lng, boolean isNetWorkTime){
        try{
            ContentValues values = new ContentValues();
            values.put(StoresManagerSettings.SupervisorVisitTaskColumns.STOREID, visitTaskInfo.mStoreId);
            values.put(StoresManagerSettings.SupervisorVisitTaskColumns.CHECKOUT_LAT, lat);
            values.put(StoresManagerSettings.SupervisorVisitTaskColumns.CHECKOUT_LNG, lng);
            values.put(StoresManagerSettings.SupervisorVisitTaskColumns.CHECKOUT_TIME, visitTaskInfo.mCheckoutTime);
            if(isNetWorkTime){
            	values.put(StoresManagerSettings.SupervisorVisitTaskColumns.CHECKOUT_TIME_STATUS, CHECK_TIME_SATUS_SERVER);
            }else{
            	values.put(StoresManagerSettings.SupervisorVisitTaskColumns.CHECKOUT_TIME_STATUS, CHECK_TIME_SATUS_APP);
            }
            values.put(StoresManagerSettings.SupervisorVisitTaskColumns.VISITED_STATUS, 1);
            if(Actions.DEBUG){
                Log.e("zhu","checkout id="+visitTaskInfo.mDbId);
            }
            if(visitTaskInfo.mDbId == -1){
                Uri uri = context.getContentResolver().insert(StoresManagerSettings.SupervisorVisitTaskColumns.CONTENT_URI, values);
                visitTaskInfo.mDbId = Long.parseLong(uri.getPathSegments().get(1));
            }else{
                context.getContentResolver().update(StoresManagerSettings.SupervisorVisitTaskColumns.getContentUri(visitTaskInfo.mDbId, false), values, null, null);
            }
            return true;
        }catch(Exception e){
            return false;
        }
    }

    /**
     * 检查输入搜索型号和PFL内容是否合法
     * @param model 型号
     * @param pfl pfl
     */
    public static boolean checkInputSearch(String model, String pfl){
      //检查内容是合法
        if((model != null && model.trim().length() == 2 && pfl != null && pfl.trim().length() == 4) || //长度型号尺寸=2 PEL=4
                (model != null && model.trim().length() == 2 && pfl != null && pfl.trim().length() == 0) || //长度型号尺寸=2 PEL=0
                (model != null && model.trim().length() == 0 && pfl != null && pfl.trim().length() == 4)){ //长度型号尺寸=0 PEL=4
            return true;
        }
        return false;
    }

    /**
     * 生成查询型号条件
     * @param model 型号
     * @param pfl pfl
     */
    public static String getSearchModelWhere(String model, String pfl){
        String where = null;
        //根据输入条件生成查询条件
        if(model != null && model.length() == 2 && pfl != null && pfl.length() == 4){
            where = StoresManagerSettings.ModelsColumns.MODELS_NAME + " LIKE \'%" + model + "-" + pfl +"%\'";
        }else if(model != null && model.length() == 2 && (pfl == null || pfl.length() == 0)){
            where = StoresManagerSettings.ModelsColumns.MODELS_NAME + " LIKE \'%" + model + "-" +"%\'";
        }else if((model == null || model.length() == 0) && (pfl != null && pfl.length() == 4)){
            where = StoresManagerSettings.ModelsColumns.MODELS_NAME + " LIKE \'%-" + pfl +"%\'";
        }
        return where;
    }

    /**
     * 获取指定type的Project
     * @param type 指定类型
     * @return list 列表
     */
    public static List<ProjectDataItem> getProjectDataList(int type){
    	if(mProjectResult.Data != null){
    		List<ProjectDataItem> list = new ArrayList<ProjectDataItem>();
    		for(SyncModel.ResProject project : mProjectResult.Data){
    			if(project != null && project.TypeId == type && project.Items != null){
    				for(SyncModel.ResProjectItem projectItem : project.Items){
    					ProjectDataItem item = new ProjectDataItem();
    					item.copyFrom(projectItem);
    					item.mType = project.TypeId;
    					list.add(item);
    				}
    			}
    		}
    		return list;
    	}
    	return null;
    }

    /**
     * 获取指定type的Project
     * @param type1 指定类型
     * @param type2 指定类型
     * @return list 列表
     */
    public static List<ProjectDataItem> getProjectDataList(int type1, int type2){
    	if(mProjectResult.Data != null){
    		List<ProjectDataItem> list = new ArrayList<ProjectDataItem>();
    		for(SyncModel.ResProject project : mProjectResult.Data){
    			if(project != null && (project.TypeId == type1 || project.TypeId == type2) && project.Items != null){
    				for(SyncModel.ResProjectItem projectItem : project.Items){
    					ProjectDataItem item = new ProjectDataItem();
    					item.copyFrom(projectItem);
    					item.mType = project.TypeId;
    					list.add(item);
    				}
    			}
    		}
    		return list;
    	}
    	return null;
    }

    /**
	 * 更具类型和ID获取project
	 * @param type 类型
	 * @param id
	 * @return ProjectDataItem
	 */
	public static ProjectDataItem getProjectDataItem(List<ProjectDataItem> list, int type, int id){
		if(list != null && list.size() > 0){
			for(ProjectDataItem item : list){
				if(item.mType == type && item.mId == id){
					return item;
				}
			}
		}
		return null;
	}

	/**
     * 读取历史数据
     * @param data 数据
     * @param isSaved 是否保存的数据
     * @param id 数据project ID
     * @param type 类型
     * @return data
     */
    public static String updateStringDataFromHistoryData(List<ProjectDataItem> list, String data, boolean isSaved, int type, int id){
    	ProjectDataItem item = null;
		if(isSaved && DataUtils.mUserData.isShowHistoryData){
			item = DataUtils.getProjectDataItem(list, type, id);
			if(item != null && item.isShow && item.isSave){
				return data;
			}
			return null;
		}
		return data;
    }

    /**
     * 读取历史数据
     * @param data 数据
     * @param isSaved 是否保存的数据
     * @param id 数据project ID
     * @return data
     */
    public static int updateIntDataFromHistoryData(List<ProjectDataItem> list, int data, boolean isSaved, int type, int id){
    	ProjectDataItem item = null;
		if(isSaved && DataUtils.mUserData.isShowHistoryData){
			item = DataUtils.getProjectDataItem(list, type, id);
			if(item != null && item.isShow && item.isSave){
				return data;
			}
			return -1;
		}
		return data;
    }

    /**
    * 读取历史数据
    * @param data 数据
    * @param isSaved 是否保存的数据
    * @param id 数据project ID
    * @return data
    */
   public static long updateLongDataFromHistoryData(List<ProjectDataItem> list, long data, boolean isSaved, int type, int id){
   	ProjectDataItem item = null;
		if(isSaved && DataUtils.mUserData.isShowHistoryData){
			item = DataUtils.getProjectDataItem(list, type, id);
			if(item != null && item.isShow && item.isSave){
				return data;
			}
			return -1;
		}
		return data;
   }

   /**
	 * 更具类型和ID获取project 是否显示
	 * @param type 类型
	 * @param id
	 * @return true 显示 否则 应藏
	 */
	public static boolean isProjectDataShow(List<ProjectDataItem> list, int type, int id){
		if(list != null && list.size() > 0){
			for(ProjectDataItem item : list){
				if(item.mType == type && item.mId == id){
					return item.isShow;
				}
			}
		}
		return true;
	}
}
